package com.yc.damai.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.yc.damai.entity.Product;
import com.yc.damai.mapper.ProductMapper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class ProductAction {

    @Resource
    ProductMapper productMapper;

    @RequestMapping("queryNews")
    public List<Product> queryNews(){
        Page<Product> page = new PageDTO<>(1, 10);
        // id 降序
        page.addOrder(OrderItem.desc("pid"));
        return productMapper.selectPage(page, null)
                .getRecords();
    }

    // 热门
    @RequestMapping("queryHots")
    public List<Product> queryHots(){
        Page<Product> page = new PageDTO<>(1, 10);
        // MyBatisPlus 提交构造器
        LambdaQueryWrapper<Product> qw = new LambdaQueryWrapper<>();
        // is_hot = 1
        qw.eq(Product::getIsHot, 1);
        return productMapper.selectPage(page, qw)
                .getRecords();
    }

    // 如果 order 宕机, 如何获取首页商品信息
}
